﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Admin_QuanLyDonDatHang_SuaDonHang : System.Web.UI.Page
{
    private DataAccess da = new DataAccess();
    string strMa = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminID"] == null)
        {
            Response.Write("<script language='javascript'>alert('Bạn phải đăng nhập / You must login');location.href='/../Admin/Login.aspx';</script>");
        }
        if (Request.QueryString["maDH"] != null && Request.QueryString["maDH"].ToString() != "")
        {
            strMa = Request.QueryString["maDH"].ToString();
        }
        if (!IsPostBack)
        {
            this.ddlMaHH.Items.Clear();
            FillDropDownList1();
            this.DDL_KH.Items.Clear();
            FillDropDownList2();
            this.GetDonHanginfo(strMa);
        } 
    }
protected void btnSua_Click(object sender, EventArgs e)
{
    EditDonHang();
}

private void EditDonHang()
{
        string strMaDH = txtMaDH.Text;
        string strMaKH = DDL_KH.SelectedValue.ToString();
        string strMaH = ddlMaHH.SelectedValue.ToString();
        int intSL = Convert.ToInt32(txtsoluong.Text);
        string strDate = txtngay.Text;
    SqlCommand comm = new SqlCommand("updateDonHang", da.SqlConn());
    comm.CommandType = CommandType.StoredProcedure;
    comm.Parameters.AddWithValue("@MaDonHang", strMaDH);
    comm.Parameters.AddWithValue("@MaKH", strMaKH);
    comm.Parameters.AddWithValue("@MaHang", strMaH);
    comm.Parameters.AddWithValue("@SoLuong", intSL);
    comm.Parameters.AddWithValue("@Ngay", strDate);

    comm.Connection.Open();
    comm.ExecuteNonQuery();

    comm.Connection.Close();
    comm.Connection.Dispose();
    Response.Redirect("DanhSachDonHang.aspx");
}
protected void lbCalendar_Click(object sender, EventArgs e)
{
    Calendar1.Visible = true;
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
    txtngay.Text = Calendar1.SelectedDate.ToString("dd-MM-yyyy"); 
    Calendar1.Visible = false;
}
private void FillDropDownList1()
{
    SqlCommand comm1 = new SqlCommand("GetAllHang", da.SqlConn());
    comm1.CommandType = CommandType.StoredProcedure;
    SqlDataReader drd1;
    comm1.Connection.Open();
    drd1 = comm1.ExecuteReader();
    while (drd1.Read())
    {
        ListItem ListNhom = new ListItem();
        ListNhom.Text = "" + drd1["TenHang"].ToString();
        ListNhom.Value = drd1["MaHang"].ToString();
        ddlMaHH.Items.Add(ListNhom);
    }
    drd1.Close();
    comm1.Connection.Close();
    comm1.Connection.Dispose();
}
private void FillDropDownList2()
{
    SqlCommand comm1 = new SqlCommand("GetAllKhachHang", da.SqlConn());
    comm1.CommandType = CommandType.StoredProcedure;
    SqlDataReader drd1;
    comm1.Connection.Open();
    drd1 = comm1.ExecuteReader();
    while (drd1.Read())
    {
        ListItem ListNhom = new ListItem();
        ListNhom.Text = "" + drd1["HoTenKH"].ToString();
        ListNhom.Value = drd1["MaKH"].ToString();
        DDL_KH.Items.Add(ListNhom);
    }
    drd1.Close();
    comm1.Connection.Close();
    comm1.Connection.Dispose();
}
protected void GetDonHanginfo(string ma)
{
    SqlDataReader drd;
    SqlCommand comm = new SqlCommand("getDonDatHangbyMa", da.SqlConn());
    comm.CommandType = CommandType.StoredProcedure;
    comm.Parameters.AddWithValue("@Ma",ma);
    comm.Connection.Open();
    drd = comm.ExecuteReader();
    if (drd.Read())
    {
        this.ddlMaHH.SelectedValue = drd["TenHang"].ToString();
        this.DDL_KH.SelectedValue = drd["HoTenKH"].ToString();
        this.txtMaDH.Text = drd["MaDonHang"].ToString();
        this.txtsoluong.Text = drd["SoLuong"].ToString();
        this.txtngay.Text = drd["NgayDatHang"].ToString();
    }
    drd.Close();
    drd.Dispose();
    comm.Connection.Close();
    comm.Connection.Dispose();

}
}
